package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.WebConstants;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysArticle;
import com.ruoyi.system.service.ISysArticleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章Controller
 *
 * @author king
 * @date 2022-07-15
 */
@RestController
@RequestMapping("/system/article")
public class SysArticleController extends BaseController {
    @Autowired
    private ISysArticleService sysArticleService;

    /**
     * 查询文章列表
     */
    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysArticle sysArticle) {
        startPage();
        List<SysArticle> list = sysArticleService.selectSysArticleList(sysArticle);
        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
    @PreAuthorize("@ss.hasPermi('system:article:export')")
    @Log(title = "导出文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysArticle sysArticle) {
        List<SysArticle> list = sysArticleService.selectSysArticleList(sysArticle);
        ExcelUtil<SysArticle> util = new ExcelUtil<SysArticle>(SysArticle.class);
        util.exportExcel(response, list, "文章数据");
    }

    /**
     * 获取文章详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sysArticleService.selectSysArticleById(id));
    }

    /**
     * 新增文章
     */
    @PreAuthorize("@ss.hasPermi('system:article:add')")
    @Log(title = "添加文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysArticle sysArticle) {
        sysArticle.setUserId(getUserId());
        return AjaxResult.success(sysArticleService.insertSysArticle(sysArticle));
    }

    /**
     * 添加草稿
     *
     * @param sysArticle
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:article:draft')")
    @Log(title = "保存草稿", businessType = BusinessType.INSERT)
    @PostMapping("/draft")
    public AjaxResult addDraft(@RequestBody SysArticle sysArticle) {
        sysArticle.setUserId(getUserId());
        return toAjax(sysArticleService.insertDraftArticle(sysArticle));
    }

    /**
     * 草稿发布
     *
     * @param sysArticle
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:article:publish')")
    @Log(title = "发布文章", businessType = BusinessType.UPDATE)
    @PutMapping("/publish")
    public AjaxResult publishArticle(@RequestBody SysArticle sysArticle) {
        sysArticle.setState(WebConstants.article.ARTICLE_PUBLISH_STATE);
        return toAjax(sysArticleService.updateSysArticle(sysArticle));
    }

    @PreAuthorize("@ss.hasPermi('system:article:top')")
    @Log(title = "置顶文章", businessType = BusinessType.UPDATE)
    @PutMapping("/top")
    public AjaxResult topArticle(@RequestBody SysArticle sysArticle) {
        SysArticle article = sysArticleService.selectSysArticleById(sysArticle.getId());
        if (WebConstants.article.ARTICLE_TOP_STATE.equals(article.getState())) {
            sysArticle.setState(WebConstants.article.ARTICLE_PUBLISH_STATE);
            sysArticleService.updateSysArticle(sysArticle);
            return AjaxResult.success("取消置顶成功!");
        }
        sysArticle.setState(WebConstants.article.ARTICLE_TOP_STATE);
        sysArticleService.updateSysArticle(sysArticle);
        return AjaxResult.success("置顶成功!");
    }


    /**
     * 修改文章
     */
    @PreAuthorize("@ss.hasPermi('system:article:edit')")
    @Log(title = "修改文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysArticle sysArticle) {
        return toAjax(sysArticleService.updateSysArticle(sysArticle));
    }

    /**
     * 删除文章(逻辑删除)
     */
    @PreAuthorize("@ss.hasPermi('system:article:remove')")
    @Log(title = "删除文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids) {
        return toAjax(sysArticleService.deleteSysArticleById(ids));
    }


    /**
     * 批量删除（物理删除）
     */
    @PreAuthorize("@ss.hasPermi('system:article:delete')")
    @Log(title = "删除文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/del/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysArticleService.deleteSysArticleByIds(ids));
    }
}
